package com.xiaoming.backend.dao.impl;

import com.xiaoming.backend.dao.IWareHouseDao;
import com.xiaoming.backend.dao.mapper.OthserMapper;
import com.xiaoming.backend.dao.mapper.WareHousePOMapper;
import com.xiaoming.backend.entity.db.po.WareHouseInfoPO;
import com.xiaoming.backend.entity.db.po.WareHousePO;
import com.xiaoming.backend.entity.orm.WareHouse;
import com.xiaoming.backend.result.exception.AppErrorException;
import com.xiaoming.backend.result.exception.error.AppError;
import com.xiaoming.backend.util.DeepCopyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class WareHouseDao implements IWareHouseDao {

    private final WareHousePOMapper wareHousePOMapper;

    private final OthserMapper othserMapper;
    @Override
    public List<WareHouse> selectWareHouse(Long facilityId) {
        List<WareHouseInfoPO> wareHouseInfos = othserMapper.getWareHouseInfo(facilityId);
        return wareHouseInfos.stream()
                .map(po -> DeepCopyUtil.copyByJackson(po, WareHouse.class))
                .collect(Collectors.toList());
    }

    @Override
    public void updateWareHouse(Integer warehouseId, Long capacity) {
        WareHousePO wareHousePO = wareHousePOMapper.selectByPrimaryKey(warehouseId);
        if (wareHousePO == null){
            throw new AppErrorException(AppError.WAREHOUSE_NOT_FIND_ERROR);
        }
        wareHousePO.setWarehouseRemainingCapacity(capacity);

        wareHousePOMapper.updateByPrimaryKey(wareHousePO);
    }
}
